package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.service.MemberService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    /**
     * 根据日期统计会员数，统计指定日期之前的会员数
     * @param month 月份
     * @return 指定月份的会员
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> month) throws ParseException {
        List<Integer> data = new ArrayList<>();
        for (String m : month) {
            Date date = DateUtils.getLastDay4Date(m, "yyyy.MM");
            Date firstDay4Date = DateUtils.getFirstDay4Date(date);
            Date lastDay4Date = DateUtils.getLastDay4Date(firstDay4Date);
            int count = memberDao.findMemberCountByMonth(firstDay4Date,lastDay4Date);
            data.add(count);
        }

        return data;
    }
}